package cn.shangqizhenshuai.sqiot.action;

import cn.shangqizhenshuai.sqiot.mqtt.MqttStarter;
import cn.shangqizhenshuai.sqiot.mqtt.Mqttservice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("mqtt")
public class MqttAction {
    @Autowired
   private MqttStarter mqttStarter;
//tcp://192.168.1.222:61613
    @ResponseBody
    @RequestMapping("/simplestart")
    public Map start(String host,String username,String password){
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();

        if(StringUtils.isNotEmpty(host)&&StringUtils.isNotEmpty(username)&&StringUtils.isNotEmpty(password)){

        try {
            mqttStarter.setstartinfo(host,"sqiop",username,password,null,null,null,null,null,null,null,null,null,null,null,null);
            Thread thread = new Thread(mqttStarter);
            thread.start();
            stringBooleanHashMap.put("data",true);
        } catch (Exception e) {
            //服务启动失败
            stringBooleanHashMap.put("errorcode","00010");
            e.printStackTrace();
        }
        }else{
            //输入启动参数不完整
            stringBooleanHashMap.put("errorcode","00020");
        }
        return stringBooleanHashMap;
    }
    @ResponseBody
    @RequestMapping("/close")
    public Map close(){
        boolean shutdown = mqttStarter.shutdown();
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        stringBooleanHashMap.put("data",shutdown);
        return stringBooleanHashMap;
    }
    @ResponseBody
    @RequestMapping("/clientstate")
    public Map clientstate (){
        boolean clientstate = mqttStarter.clientstate();
        HashMap<String, Object> stringBooleanHashMap = new HashMap<String, Object>();
        stringBooleanHashMap.put("data",clientstate);
        return stringBooleanHashMap;
    }



}
